/**********************************************
** @Des: This file ...
** @Author: xiaomeng79
** @Date:   17-11-16 下午5:29
***********************************************/
package controllers

import (
	"github.com/astaxie/beego"
	"strings"
	"github.com/xiaomeng79/go-rbac/libs"
	"strconv"
	"github.com/xiaomeng79/go-rbac/models"
)

const (
	MSG_OK  = 0
	MSG_ERR = -1
)

type BaseController struct {
	beego.Controller
	controllerName string
	actionName     string
	adminId  int //用户ID
	pageSize       int //每页大小
	page	int //页数
}

//前期准备
func (this *BaseController) Prepare() {
	//如果有分页显示的，处理分页
	page, err := this.GetInt("page")
	if err != nil {
		page = 1
	}
	this.page = page
	limit, err := this.GetInt("limit")
	if err != nil {
		limit = 30
	}
	this.pageSize = limit
	//识别控制器和方法
	controllerName, actionName := this.GetControllerAndAction()
	this.controllerName = strings.ToLower(controllerName[0 : len(controllerName)-10])
	this.actionName = strings.ToLower(actionName)
	this.Data["version"] = beego.AppConfig.String("version")
	this.Data["siteName"] = beego.AppConfig.String("site.name")
	//设置XSRF
	this.Data["xsrf_token"] = this.XSRFToken()
	//判断用户是否登录
	this.Is_Login()
	//判断用户是否有权限
	this.Is_Auth()
}

/**
判断是否登录
 */
func (this *BaseController) Is_Login() {
	allow_url := []string{"admin/login","admin/logout"}//不用登录就可以连接
	url := this.controllerName + "/" +this.actionName
	this.adminId,_ = strconv.Atoi(libs.InterfaceToString(this.GetSession("admin_id")))
	if !libs.Str_In_Array(url,allow_url) && this.adminId == 0 {
		this.redirect(beego.URLFor("AdminController.Login"))
	}
	//session判断用户是否登录
	this.Data["admin_username"] = libs.InterfaceToString(this.GetSession("admin_username"))
}

/**
判断用户是否有权限
 */
func (this *BaseController) Is_Auth() {
	// 如果是超级管理员,就不需要权限认证
	if this.adminId == 1 {
		return
	}
	allow_url := []string{"admin/login","admin/logout"}//不用登录就可以连接
	url := this.controllerName + "/" +this.actionName
	if !libs.Str_In_Array(url,allow_url) {
		Admin := new(models.Admin)
		res := Admin.Is_Auth(this.adminId,this.controllerName,this.actionName)
		if !res {
			if this.IsAjax() {
				this.ajaxMsg("没有权限",MSG_ERR)
			} else {
				this.Ctx.WriteString("没有权限")
				//this.Abort("401")
			}
		}
	}
}

// 是否POST提交
func (this *BaseController) isPost() bool {
	return this.Ctx.Input.IsPost()
}

// 是否AJAX提交
func (this *BaseController) IsAjax() bool {
	return this.Ctx.Input.IsAjax()
}

//获取用户IP地址
func (this *BaseController) getClientIp() string {
	s := strings.Split(this.Ctx.Request.RemoteAddr, ":")
	return s[0]
}

// 重定向
func (this *BaseController) redirect(url string) {
	this.Redirect(url, 302)
	this.StopRun()
}

//加载模板
func (this *BaseController) display(tpl ...string) {
	var tplname string
	if len(tpl) > 0 {
		tplname = strings.Join([]string{tpl[0], "html"}, ".")
	} else {
		tplname = this.controllerName + "/" + this.actionName + ".html"
	}
	this.TplName = tplname
}

//ajax返回
func (this *BaseController) ajaxMsg(msg interface{}, msgno int) {
	out := make(map[string]interface{})
	out["status"] = msgno
	out["message"] = msg
	this.Data["json"] = out
	this.ServeJSON()
	this.StopRun()
}

//ajax返回 列表
func (this *BaseController) ajaxList(msg interface{}, msgno int, count int64, data interface{}) {
	out := make(map[string]interface{})
	out["code"] = msgno
	out["msg"] = msg
	out["count"] = count
	out["data"] = data
	this.Data["json"] = out
	this.ServeJSON()
	this.StopRun()
}